package ui;


import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import ui.mainwidget.Login;
import ui.resource.ResourceManager;
import ui.resource.ResourceURL;


/**
 * 
 * This class is the entrance of the program. 
 * It also defines some basic parameters.
 * I think it is better to define constants in functions rather than enums.
 * 
 * @author wjp
 * @since 2013/3/4
 * 
 * */

public class MainWindow{
    private static Shell 						shell=new Shell(Display.getDefault());
    private static Composite                    currentMainWidget=new Composite(shell, SWT.NULL);
    public static int 							getStandardWidth()
    {
    	return 1024;
    }
    public static int 							getStandardHeight()
    {
    	return 512;
    }
	public static Shell 						getShell()
	{
		return shell;
	}
	public static void			 				setMainWidget(Composite mainWidget)
	{
		currentMainWidget.dispose();
		currentMainWidget=mainWidget;
		mainWidget.setParent(shell);
		Point size = mainWidget.getSize();
		if(size.x == 0 && size.y == 0) 
		{
			mainWidget.pack();
			shell.pack();
		}
		else
		{
			Rectangle bounds = shell.computeTrim(0, 0, size.x, size.y);
			shell.setSize(bounds.width, bounds.height);
		}
	}
	public static void							 main(String[] args) 
	{
		shell.setText("\u65c5\u884c\u8005");
		shell.setImage(ResourceManager.getImage(ResourceURL.getApplicationIconURL()));
		shell.setLayout(new FillLayout());
		shell.layout();	
		setMainWidget(new Login(shell, SWT.DOUBLE_BUFFERED));
		shell.open();
		Display display=Display.getCurrent();
		while (!shell.isDisposed()) 
		{
			if (!display.readAndDispatch())
			{
				display.sleep();
			}
		}

	}

}
